// miniprogram/pages/room/roomDetail.js
const db = wx.cloud.database()
const regeneratorRuntime = require('../../libs/runtime')
const moment = require('../../libs/moment.js')
const {
    $Message
} = require('../../components/base/index.js');
Page({
    data: {
        startDate: "",
        endDate: "",
        roomId: "",
    },
    async onLoad(options) {
        wx.showLoading({
            title: '加载中',
        })
        let that = this
        if (options.roomId && options.endDate && options.startDate) {
            await db.collection('room_unit_list').doc(options.roomId).get({
                success(res) {
                    let b = that.isOffDay(options.startDate)
                    if (b) {
                        res.data.currentPrice = +res.data.currentPrice + 100
                    }
                    if (that.isShowToast(options.startDate, options.endDate)) {
                        if (b) {
                            // 如果是周末入住 工作日离店
                            $Message({
                                content: '注：周日至周四当天价格为￥' + (+res.data.currentPrice - 100),
                                type: 'warning',
                                duration: 10
                            });
                        } else {
                            // 如果是 工作日入住 周末离店
                            $Message({
                                content: '注：周五/周六当天价格为￥' + (+res.data.currentPrice + 100),
                                type: 'warning',
                                duration: 10
                            });
                        }
                    }
                    that.setData({
                        endDate: options.endDate,
                        startDate: options.startDate,
                        roomDetail: res.data,
                        roomDetailImgs: res.data.roomDetailImgs,
                    })
                },
                fail(err) {
                    wx.showToast({
                        title: err,
                        icon: "none"
                    })
                },
                complete() {
                    wx.hideLoading()
                }
            })
        } else {
            wx.hideLoading()
            wx.showToast({
                icon: 'none',
                title: '未获取到参数',
            })
        }
    },
    // 商品下单
    async makeOrder(e) {
        wx.showLoading({
            title: '正在下单',
        })
        const goodId = e.target.dataset.goodid

        let isFull = await db.collection('room_unit_list').doc(goodId).get();
        if (isFull.data.isRoomTypeFull) {
            wx.hideLoading()
            wx.showToast({
                title: '房间已满，下单失败',
                icon: 'none'
            })
            return
        }
        try {
            const {
                result
            } = await wx.cloud.callFunction({
                name: 'pay',
                data: {
                    type: 'unifiedorder',
                    data: {
                        startDate: this.data.startDate,
                        endDate: this.data.endDate,
                        goodId: goodId
                    }
                }
            })
            const data = result.data
            console.log(result)
            wx.hideLoading()
            wx.navigateTo({
                url: `/pages/pay-result/index?id=${data.out_trade_no}&roomId=${goodId}&startDate=${this.data.startDate}&endDate=${this.data.endDate}`
            })
        } catch (err) {
            wx.hideLoading()
            wx.showToast({
                title: '下单失败，请重试',
                icon: 'none'
            })
        }
    },
    //判断是否为周末
    isOffDay(date) {
        let d = new Date(date)
        let day = d.getDay()
        if (day >= 5 && day <= 6) {
            return true
        }
        return false
    },
    //获取入住天数
    isShowToast(startDate, endDate) {
        let day = new Date(startDate).getDay()
        let dayDiff = moment(endDate).diff(moment(startDate), 'days')
        return (day + dayDiff) > 5
    },

})